package chu.dejian.widget.chartView;

import android.content.Context;

import org.achartengine.renderer.XYMultipleSeriesRenderer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sam.blemodule.R;
import chu.dejian.entity.RecordEntity;
import com.sam.utils.time.TimeUtils;

/**
 * Created by Administrator on 2015/12/19 0019.
 */
public class ThreeMonthBPChartUtils extends BaseBPChartUtils {
    private RecordEntity[] records = new RecordEntity[93];
    private List<List<RecordEntity>> groupedRecordLists = new ArrayList<>();
    public ThreeMonthBPChartUtils(Context context) {
        super(context);
    }

    public ThreeMonthBPChartUtils(Context context, int lineColor, boolean showPoint) {
        super(context, lineColor, showPoint);
    }

    @Override
    protected XYMultipleSeriesRenderer getChartRenderer() {
        XYMultipleSeriesRenderer renderer =  super.getChartRenderer();
        renderer.setXAxisMin(1);
        renderer.setXAxisMax(93);
        return renderer;
    }

    @Override
    public void setRecordEntityList(List<RecordEntity> recordEntityList) {
        if(recordEntityList == null || recordEntityList.size() == 0) return;
        for(int i = 0; i < 93; i++) groupedRecordLists.add(new ArrayList<RecordEntity>());
        int size = recordEntityList.size();
        double[] sysArray = new double[93];
        double[] diaArray = new double[93];
        for(int i = 0; i < size; i++){
            final RecordEntity recordEntity = recordEntityList.get(i);
            if(TimeUtils.isLastThreeMonth(recordEntity.getMeasureDateTime())) {
                int dayIndex = TimeUtils.getDayIndexInLastThreeMonth(recordEntity.getMeasureDateTime());
                sysArray[dayIndex] = recordEntity.systolic;
                diaArray[dayIndex] = recordEntity.diastolic;
                if(getShowDayReacordWay() == SHOW_DAY_REACORD_WAY.LAST || (getShowDayReacordWay() == SHOW_DAY_REACORD_WAY.FIRST && records[dayIndex] == null))
                records[dayIndex] = recordEntity;
                groupedRecordLists.get(dayIndex).add(recordEntity);
            }
        }
        addLine(getPointDataSeries("SYS",sysArray), getLineSeriesRender(lineColor), getSysLevelRender());
        addLine(getPointDataSeries("DIA",diaArray), getLineSeriesRender(lineColor2), getDiaLevelRender());

        Map<Double,String> xFirstLabelsMap = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat(mContext.getResources().getString(R.string.which_month));
        Date date = new Date();
        xFirstLabelsMap.put(15d,mContext.getResources().getString(R.string.last_two_month));
        xFirstLabelsMap.put(46d,mContext.getResources().getString(R.string.last_month));
        xFirstLabelsMap.put(77d,mContext.getResources().getString(R.string.current_month));
        setXFirstLabels(xFirstLabelsMap);

        Map<Double,String> xSecondLabelsMap = new HashMap<>();
        xSecondLabelsMap.put(15d,sdf.format(TimeUtils.getLastDate(date, 2)));
        xSecondLabelsMap.put(46d,sdf.format(TimeUtils.getLastDate(date, 1)));
        xSecondLabelsMap.put(77d,sdf.format(date));
        setXSecondLabels(xSecondLabelsMap);
    }

    @Override
    public List<RecordEntity> getShownRecordList() {
        return Arrays.asList(records);
    }

    @Override
    public List<List<RecordEntity>> getGroupedRecordLists() {
        return groupedRecordLists;
    }
}
